package com.csht.service.Impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csht.mapper.CshtTradingMapper;
import com.csht.model.CshtTrading;
import com.csht.page.PagerModel;
import com.csht.service.CshtTradingService;
import com.csht.vo.my.CshtTradingVo;

/**
 * 交易中的商品service
 * @author chenhuiyan
 *
 */
@Service
public class CshtTradingServiceImpl implements CshtTradingService {

	@Autowired
	private CshtTradingMapper cshtTradingMapper;
	
	/**
	 * 根据用户id查找正处于交易中的商品数量
	 */
	@Override
	public Integer getTradingGoodsNumberByUserId(Integer userId) throws Exception {
		Integer count = cshtTradingMapper.getTradingGoodsNumberByUserId(userId);
		return count;
	}

	/**
	 * 根据用户id查找交易中的商品
	 */
	@Override
	public PagerModel<CshtTradingVo> getTradingByUserId(Map<String, Integer> res) throws Exception {
		List<CshtTradingVo> cshtTradingVoList = cshtTradingMapper.getTradingByUserId(res);
		PagerModel<CshtTradingVo> pagerModel = new PagerModel<CshtTradingVo>();
		Integer totalSize = this.getTradingGoodsNumberByUserId(res.get("userId"));
		Integer messagePageSize = res.get("messagePageSize");
		Integer offset = res.get("offset");
		pagerModel.setDates(cshtTradingVoList);
		pagerModel.setMessagePageSize(messagePageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据id查找交易中的商品
	 */
	@Override
	public CshtTrading selectTradingById(Integer tradingId) throws Exception {
		CshtTrading cshtTrading = cshtTradingMapper.selectTradingById(tradingId);
		return cshtTrading;
	}

	/**
	 * 根据id删除交易中的商品
	 */
	@Override
	public void deleteTradingById(Integer tradingId) throws Exception {
		cshtTradingMapper.deleteTradingById(tradingId);
	}

	/**
	 * 根据id查找交易中的商品的原商品id
	 */
	@Override
	public Integer selectTradingGoodsIdById(Integer tradingId) throws Exception {
		Integer goodsId = cshtTradingMapper.selectTradingGoodsIdById(tradingId);
		return goodsId;
	}

	/**
	 * 插入一条记录
	 */
	@Override
	public void insertTradingGoods(CshtTrading cshtTrading) throws Exception {
		cshtTradingMapper.insertTradingGoods(cshtTrading);
	}

}
